/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reader;

import java.util.Arrays;
import java.util.Collection;
import java.util.ListIterator;
import java.util.Vector;

/**
 *
 * @author Danzz
 */
public class DataType {
    public static enum TypeEnum {numeric, nominal, string, date} // dapat berupa salah satu dari empat tipe
    
    public TypeEnum dataType; // tipe data yang dipake
    public Vector<String> typeNominal; // vector untuk menyimpan nilai nominal
    public String dateFormat; // string format untuk tipe date
    
    // tambahan
    public String deskripsiPertanyaan; // list pertanyaan yang bersesuaian,
    
    public DataType(String type) throws Exception {
        deskripsiPertanyaan = "";
        if (TypeEnum.numeric.toString().equalsIgnoreCase(type)) {
            dataType = TypeEnum.numeric;
        } else if (TypeEnum.string.toString().equalsIgnoreCase(type)) {
            dataType = TypeEnum.string;
        } else if (type.charAt(0)=='{') {
            dataType = TypeEnum.nominal;
            this.putNominalValue(type);
        } else if (TypeEnum.date.toString().equalsIgnoreCase(type.substring(0, 4))) {
            dataType = TypeEnum.date;
            // baca format -> kayaknya ga bakal ada yg tipe gini ^^
        } else {
            throw new Exception("Tidak ada tipe yang sesuai \""+type+"\"");
        }
    }
    
    void putNominalValue(String val) throws Exception {
        if (val.startsWith("{") && val.endsWith("}")) {
            typeNominal = new Vector<String>();
            String parse1 = val.substring(1, val.length()-1);
            String[] parse2 = parse1.split(","); // asumsi ga ada nilai dalam quote pake koma
            for(String parse3 : parse2) {
                if (!parse3.startsWith("'")) {
                    typeNominal.add(parse3);
                } else if (parse3.startsWith("'") && parse3.endsWith("'")) {
                    typeNominal.add(parse3.substring(1, parse3.length()-1));
                } else {
                    throw new Exception("Nilai tipe nominal kurang '"+"\""+val+"\"");
                }
            }
        } else {
            throw new Exception("List nominal value salah"+"\""+val+"\"");
        }
    }
    
    // tambahan
    public void putPertanyaan(String val) throws Exception {
        if (val.startsWith("\"") && val.endsWith("\"")) {
            String parse1 = val.substring(1, val.length()-1);
            deskripsiPertanyaan = parse1;
        } else {
            throw new Exception("Format Pertanyaan salah"+"\""+val+"\"");
        }
    }
    
    public String printPertanyaan() {
        if (deskripsiPertanyaan.isEmpty()) {
            return "Tidak ada pertanyaan yang bersesuaian";
        }
        
        return deskripsiPertanyaan;
    }
    
    public int getJumlahNominal() {
        if (!dataType.equals(TypeEnum.nominal))
            return 1;
        return typeNominal.size();
    }
    
    public String getNominalAtIdx(int idx){
        if (idx>=typeNominal.size())
            return "";
        return typeNominal.get(idx);
    }
    
    public int getIndexNominalValue(String val) {
        if (!typeNominal.contains(val))
            return typeNominal.size()+1;
        return typeNominal.indexOf(val);
    }
    
    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder();
        if (dataType.equals(TypeEnum.nominal)) {
            ListIterator iter = typeNominal.listIterator();
            ret.append("{");
            while (iter.hasNext()) {
                ret.append((String) iter.next());
                if (iter.hasNext())
                    ret.append(",");
            }
            ret.append("}");
        } else
            ret.append(dataType.toString());
        return ret.toString();
    }
}
